﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DocPlat.Library;
using DataGridViewAutoFilter;

namespace DocPlat.UI
{
    public partial class PaymentFormInputPanel : Form
    {
        ServerObject mServerObject = null;
        int mNrBonDePlata = 0;
        bool mEditOperation = false;
        bool mOpenPrintFormAfterSave = false;


        public int NrBonDePlata
        {
            get { return mNrBonDePlata; }
            set { mNrBonDePlata = value; }
        }

        public bool EditOperation
        {
            get { return mEditOperation; }
            set { mEditOperation = value; }
        }

        public bool OpenPrintFormAfterSave
        {
            get { return mOpenPrintFormAfterSave; }
            set { mOpenPrintFormAfterSave = value; }
        }


        private void FillBeneficiarComboBox()
        {
            DataTable benefisList = mServerObject.GetCompanyList(true);
            beneficiarComboBox.DataSource = null;
            if (benefisList != null && benefisList.Rows.Count > 0)
            {
                beneficiarComboBox.DataSource = benefisList;
                beneficiarComboBox.ValueMember = "CodFiscal";
                beneficiarComboBox.DisplayMember = "Name";
            }
        }

        private void FillRechizBankComboBox()
        {
            DataTable rechizList = mServerObject.GetBankList();
            rechizBankComboBox.DataSource = null;
            if (rechizList != null && rechizList.Rows.Count > 0)
            {
                rechizBankComboBox.DataSource = rechizList;
                rechizBankComboBox.ValueMember = "id";
                rechizBankComboBox.DisplayMember = "Description";
            }
        }

        private void FillServiceListComboBox()
        {
            DataTable servList = mServerObject.GetServicesList();
            serviceListComboBox.DataSource = null;
            if (servList != null && servList.Rows.Count > 0)
            {
                serviceListComboBox.DataSource = servList;
                serviceListComboBox.DisplayMember = "Description";
                serviceListComboBox.ValueMember = "id";
            }
        }

        public PaymentFormInputPanel()
        {
            InitializeComponent();
            mServerObject = new ServerObject();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void FillServicesDataGridView(string idDocPlat)
        {
            int intIDDocPlat = 0;
            int.TryParse(idDocPlat, out intIDDocPlat);

            if (intIDDocPlat != -1)
            {
                DataTable sourceTable = mServerObject.GetServiceFromDocPlatList(intIDDocPlat);
                servicesDataGridView.DataSource = null;

                if (sourceTable != null && sourceTable.Rows.Count > 0)
                {
                    servicesDataGridView.DataSource = sourceTable;

                    servicesDataGridView.Columns["idDocPlat"].Visible = false;
                    servicesDataGridView.Columns["idService"].Visible = false;

                    double summtotal = 0;
                    double summTVA = 0;

                    for (int i = 0; i < sourceTable.Rows.Count; i++)
                    {                       
                        double tempSumm = 0;
                        double.TryParse(sourceTable.Rows[i]["Cost"].ToString(), out tempSumm );
                        summtotal += tempSumm;
                    }

                    totalFaraTVA_Label.Text = summtotal.ToString();
                    tva_Label.Text = summTVA.ToString();
                    totalInclusivTVA_label.Text = summtotal.ToString();
                }
            }
        }

        private void PaymentFormInputPanel_Load(object sender, EventArgs e)
        {
            nrBonDePlataTextBox.Text = mNrBonDePlata.ToString();
            FillBeneficiarComboBox();
            FillRechizBankComboBox();
            FillServiceListComboBox();
            dateTextBox.Text = DateTime.Now.ToString("dd.MM.yyyy");

            if (mEditOperation && mNrBonDePlata != 0)
            {
                DataTable dataTable = mServerObject.GetDocPlatByID(mNrBonDePlata);

                if (dataTable != null && dataTable.Rows.Count == 1)
                { 
                    nrBonDePlataTextBox.Text = dataTable.Rows[0]["id"].ToString();
                    dateTextBox.Text = dataTable.Rows[0]["date"].ToString();

                    if (beneficiarComboBox.DataSource != null)
                    {
                        beneficiarComboBox.SelectedValue = dataTable.Rows[0]["cod_fisc_benef"].ToString();
                    }

                    if (rechizBankComboBox.DataSource != null)
                    {
                        rechizBankComboBox.SelectedValue = dataTable.Rows[0]["ID Banca"].ToString();
                    }

                    DataTable infoPlatitor = mServerObject.GetPayerByID(dataTable.Rows[0]["idnoPlatitor"].ToString());
                    if (infoPlatitor != null && infoPlatitor.Rows.Count == 1)
                    {
                        platitorulTextBox.Text = infoPlatitor.Rows[0]["FirstName"].ToString() + " " + infoPlatitor.Rows[0]["LastName"].ToString() + " " + infoPlatitor.Rows[0]["Patronimic"].ToString();
                        idnoTextBox.Text = infoPlatitor.Rows[0]["IDNO"].ToString();
                    }

                    DataTable serviceList = mServerObject.GetServiceFromDocPlatList(mNrBonDePlata);
                    if (serviceList != null && serviceList.Rows.Count > 0)
                    {
                        servicesDataGridView.DataSource = serviceList;

                        servicesDataGridView.Columns["idDocPlat"].Visible = false;
                        servicesDataGridView.Columns["idService"].Visible = false;

                        double summtotal = 0;
                        double summTVA = 0;

                        for (int i = 0; i < serviceList.Rows.Count; i++)
                        {
                            double tempSumm = 0;
                            double.TryParse(serviceList.Rows[i]["Cost"].ToString(), out tempSumm);
                            summtotal += tempSumm;
                        }

                        totalFaraTVA_Label.Text = summtotal.ToString();
                        tva_Label.Text = summTVA.ToString();
                        totalInclusivTVA_label.Text = summtotal.ToString();
                    }
                } 
            }
        }

        private void platitorulTextBox_MouseClick(object sender, MouseEventArgs e)
        {
            Payer payerForm = new Payer();
            payerForm.CloseAfterSave = true;
            payerForm.NewRecord = true;

            if (payerForm.ShowDialog() == DialogResult.OK)
            {
                platitorulTextBox.Text = payerForm.SelectedFirstName + " " + payerForm.SelectedLastName;
                idnoTextBox.Text = payerForm.SelectedIDNO.ToString();
            }
        }

        private void beneficiarComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (beneficiarComboBox.SelectedValue != null)
            {
                codFiscTextBox.Text = beneficiarComboBox.SelectedValue.ToString();
            }
        }

        private void rechizBankComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rechizBankComboBox.SelectedValue != null)
            {
                int idBank = 0;
                int.TryParse(rechizBankComboBox.SelectedValue.ToString(), out idBank);

                if (idBank != 0)
                {
                    DataTable rechiz = mServerObject.GetBankByID(idBank);
                    if (rechiz != null && rechiz.Rows.Count == 1)
                    {
                        contTextBox.Text = rechiz.Rows[0]["Cont"].ToString();
                    }
                }            
            }
        }

        private void addServiceButton_Click(object sender, EventArgs e)
        {
            if (serviceListComboBox.SelectedValue != null && !nrBonDePlataTextBox.Text.Equals(string.Empty))
            {
                int idDocPlat = 0;
                int idService = 0;
                int.TryParse(nrBonDePlataTextBox.Text, out idDocPlat);
                int.TryParse(serviceListComboBox.SelectedValue.ToString(), out idService);

                if (mServerObject.AddNewServiceInDocPlat(idDocPlat, idService))
                {
                    FillServicesDataGridView(idDocPlat.ToString());
                }
                else
                {
                    MessageBox.Show("Sorry, service not added!","Error!");
                }                
            }
        }

        private void deleteService_Click(object sender, EventArgs e)
        {
            if (servicesDataGridView.SelectedRows.Count == 1 && MessageBox.Show("Are you sure ???", "Attention!", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                int idDocPlat = 0;
                int idService = 0;
                int.TryParse(servicesDataGridView.Rows[servicesDataGridView.SelectedRows[0].Index].Cells["idDocPlat"].Value.ToString(), out idDocPlat);
                int.TryParse(servicesDataGridView.Rows[servicesDataGridView.SelectedRows[0].Index].Cells["idService"].Value.ToString(), out idService);

                if (idDocPlat != 0 && idService != 0 && mServerObject.DeleteServiceInDocPlat(idDocPlat, idService))
                {
                    FillServicesDataGridView(idDocPlat.ToString());
                }
                else
                {
                    MessageBox.Show("Sorry, service not Deleted!", "Error!");
                }
            }
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            if (!nrBonDePlataTextBox.Text.Equals(string.Empty)
                && !dateTextBox.Text.Equals(string.Empty)
                && !codFiscTextBox.Text.Equals(string.Empty)
                && !contTextBox.Text.Equals(string.Empty)
                && !idnoTextBox.Text.Equals(string.Empty)
                && !platitorulTextBox.Text.Equals(string.Empty)
                && beneficiarComboBox.SelectedValue != null
                && rechizBankComboBox.SelectedValue != null)
            {
                int idBonPlat = 0;
                int.TryParse(nrBonDePlataTextBox.Text, out idBonPlat);
                
                DateTime editDate = new DateTime(1,1,1);
                DateTime.TryParse(dateTextBox.Text, out editDate);

                double summaTotala = 0;
                double.TryParse(totalInclusivTVA_label.Text, out summaTotala);


                if (mServerObject.SaveBonPlat(idBonPlat, editDate, codFiscTextBox.Text, rechizBankComboBox.SelectedValue.ToString(), idnoTextBox.Text, summaTotala))
                {
                    this.DialogResult = DialogResult.OK; mOpenPrintFormAfterSave = true;
                }
                else
                {
                    this.DialogResult = DialogResult.Abort;
                }            
            } 
            else
            {
                MessageBox.Show("Please, fill all parameters!","Attention!");
            }
        }
    }
}
